<?php
$table_name = "book";

//header( 'Content-Type: text/html; charset=utf-8' );
header( 'Content-Type: application/json; charset=utf-8' );

require_once ("../conf/conf.php");
require_once ("../locales/ro.php");
require ("../include/constants.php");
require ("../include/function.php");

$connection = db_connect(DB_HOST, DB_NAME, DB_USER, DB_PASSWORD);

$action = isset ($_POST['action']) ? $_POST['action'] : (isset($_GET['action']) ? $_GET['action'] : "get_books");

session_start();

if($action == 'get_books'){
	echo getBooks();
} else if($action == 'get_themes'){
    echo getThemes();
} else if($action == 'edit'){
    if(isset($_SESSION['user_id'])) {
        echo editTheme();
    } else {
        echo renderUnauthorized();
    }
} else if($action == 'edit_list'){
    if(isset($_SESSION['user_id'])) {
        echo editList();
    } else {
        echo renderUnauthorized();
    }
} else {
	echo renderExpectationFailed();
}

db_disconnect($connection);


// ====================================================
function getBooks(){
	global $table_name;
	$sql = "SELECT id, name, (SELECT max(number) FROM chapter WHERE book_id = $table_name.id) as chapters FROM $table_name";
    $records = sql2JSONArray($sql);
    return renderRecords($records);
}


function getThemes(){
    $userId = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : -1; // -1 no user
    $book = (int)$_GET['book'];

//    $sql = "SELECT t.id, c.number, t.theme FROM chapter AS c LEFT JOIN user_chapters_theme as t ON (t.chapter_id = c.id AND t.user_id = $userId) WHERE c.book_id = $book";
    $sql = "SELECT c.number, t.theme FROM chapter AS c LEFT JOIN user_chapters_theme as t ON (t.chapter_id = c.id AND t.user_id = $userId) WHERE c.book_id = $book";
    $records = sql2JSONArray($sql);

    return renderRecords($records);
}

// ====================================================
function editTheme(){
    $userId = $_SESSION['user_id'];
    $book = (int)$_POST['book'];
    $chapter = (int)$_POST['chapter'];
    // TODO verify $value for SQL Injection
    $theme = trim($_POST['theme']);

    return setTheme($userId, $book, $chapter, $theme);
}

function editList(){
    $userId = $_SESSION['user_id'];
    $book = (int)$_GET['book'];
    $chapter = (int)$_POST['id'];

    // TODO verify $value for SQL Injection
    $theme = trim($_POST['value']);

    return setTheme($userId, $book, $chapter, $theme);
}

/**
 * @private
 */
function setTheme($userId, $book, $chapter, $theme){
    if($book == 0 || $chapter == 0){
        return renderExpectationFailed();
    }

    $data = getUserBookTheme($userId, $book, $chapter);

    if($data) {
        $sql = "UPDATE user_chapters_theme set theme = '$theme' WHERE id = ".$data[0];
        $r = do_query($sql);
        $i = mysql_affected_rows();
        if($i == 1){
            return renderSuccess();
        }
    } else {
        $sql = "INSERT INTO user_chapters_theme(theme, user_id, chapter_id) SELECT '$theme' as theme, $userId as user_id, c.id FROM chapter AS c WHERE c.book_id = $book AND c.number = $chapter";
        $r = do_query($sql);
        if($r == 1){
            return renderSuccess();
        }
    }

    return renderError("Tema capitolui nu a putut fi salvată!"); //TODO i18n
}


?>